package lzzy8;

public class GenericSort {
    public static void main(String[] args) {
        Integer[] intArray = {2,4,3};
        Double[] doubleArray = {3.4, 1.3, -22.1};
        Character[] charArray = {'a','J','r'};
        String[] stringArray = {"Tom","Susan","Kim","wgr"};
        Student[] studentArray = {
                new Student("1","韦国荣","20200317029",85),
                new Student("2","www","26580",90),
                new Student("3","rrr","81913043",65)};

        sort(intArray);
        sort(doubleArray);
        sort(charArray);
        sort(stringArray);
        sort(studentArray);

        System.out.println("Sorted Integer objects：");
        printList(intArray);
        System.out.println("Sorted Double objects：");
        printList(doubleArray);
        System.out.println("Sorted Character objects：");
        printList(charArray);
        System.out.println("Sorted String objects：");
        printList(stringArray);
        System.out.println("Sorted Student objects：");
        printList(studentArray);
    }

    public static <E extends Comparable<E>> void sort(E[] list
    ) {
        E currentMin;
        int currentMinIndex;

        for (int i = 0; i < list.length - 1; i++) {
            currentMin = list[i];
            currentMinIndex = i;

            for (int j = i + 1; j < list.length; j++) {
                if (currentMin.compareTo(list[j])>0){
                    currentMin=list[j];
                    currentMinIndex=j;
                }
            }
            if (currentMinIndex!=i){
                list[currentMinIndex]=list[i];
                list[i]=currentMin;
            }
        }
    }

    private static void printList(Object[] list) {
        for (int i = 0; i< list.length; i++){
            System.out.println(list[i] + " ");
        }
        System.out.println();
    }
}